<?php
	/* This will be run first thing on every page, so here is where we configure the environment */
	
	//start session on every page
	session_start();
	
	//website address - running in either development(locahost) or production(online) mode
	if ($_SERVER["HTTP_HOST"] == "localhost") {
		define('DB_SERVER', 'localhost');
		define('DB_SERVER_USERNAME', 'root');
		define('DB_SERVER_PASSWORD', '');
		define('DB_DATABASE', 'sablogawards_db1');
		define('FILE_SERVER', 'c:/www/sablogawards/');
		define('HTTP_SERVER', 'http://localhost/sablogawards/');
		define('HTTP_COOKIE_DOMAIN', 'localhost');
		define('HTTP_COOKIE_PATH', '/sablogawards/');
	} else {
		define('DB_SERVER', 'dedi18.cpt2.host-h.net');
		define('DB_SERVER_USERNAME', 'sabloga_1');
		define('DB_SERVER_PASSWORD', 'nrbP9i68');;
		define('DB_DATABASE', 'sabloga_db1');
		define('FILE_SERVER', '/usr/www/users/sabloga/');
		define('HTTP_SERVER', 'http://2007.sablogawards.com/');
		define('HTTP_COOKIE_DOMAIN', 'http://2007.sablogawards.com');
		define('HTTP_COOKIE_PATH', '/');
	}
	
	//include and setup active db connection
	include_once("db.class.php");
	$db = new CDatabase();
	$db->Connect();
	
	//restore session from cookie if necessary
	if(!isset($_SESSION['loggedin']) && isset($_COOKIE['loggedin'])) {
		if($db->Query("SELECT * FROM member WHERE username = '".$_COOKIE['loggedin']."';")) {
			$db->getRow();
			$_SESSION['loggedin'] = $user;
			$_SESSION['email'] = $db->access['email'];
			$_SESSION['access'] = $db->access['access'];
		}
	}
	
	//get admin name and email for contact emails and stuff
	if($db->Query("SELECT name, email FROM member WHERE access = '0';")) {
		$db->getRow();
		define('MASTER_NAME', $db->access['name']);
		define('MASTER_EMAIL', $db->access['email']);
	}
	
	//get datails for the latest active contest
	if($db->Query("SELECT * FROM contest WHERE status = 'active' ORDER BY nominate_from DESC;")) {
		$db->getRow();
		define('CONTEST_ID', $db->access['id']);
		define('CONTEST_NAME', $db->access['name']);
		define('CONTEST_STATUS', $db->access['status']);
		define('NOMINATE_FROM', $db->access['nominate_from']);
		define('NOMINATE_TO', $db->access['nominate_to']);
		define('VOTE_FROM', $db->access['vote_from']);
		define('VOTE_TO', $db->access['vote_to']);
		define('PREPARATION_PHASE', (NOMINATE_FROM > mktime())?true:false);
		define('NOMINATION_PHASE', (NOMINATE_FROM < mktime() && NOMINATE_TO > mktime())?true:false);
		define('JUDGING_PHASE', (NOMINATE_TO < mktime() && VOTE_FROM > mktime())?true:false);
		define('VOTING_PHASE', (VOTE_FROM < mktime() && VOTE_TO > mktime())?true:false);
		define('FINISHED_PHASE', (VOTE_TO < mktime())?true:false);
	} else $display_last_contest_result = true;
	
	//other constants
	define('SALT', 'iD');
	define('WAP_DIR', 'mobile/');
	define('FILE_DIR', 'files/');
	define('IMG_DIR', 'files/images/');
	define('STYLE_DIR', 'styles/');
	define('SCRIPT_DIR', 'jscripts/');
	define('SERVER_DIR', 'server/');
	define('INCLUDE_DIR', 'includes/');
	define('ADMIN_DIR', 'admin/');
	
	//default meta tags
	$meta_title = "SA Blog Awards - 2007";
	$meta_keywords = "South Africa, Blog, Awards, 2007";
	$meta_description = "2007 South African Blog Awards";
	
	//default included js and styles
	$js = "<script type='text/javascript' src='".HTTP_SERVER.SCRIPT_DIR."utils/prototype.js'></script>
				 <script type='text/javascript' src='".HTTP_SERVER.SCRIPT_DIR."utils/scriptaculous.js'></script>
				 <script type='text/javascript' src='".HTTP_SERVER.SCRIPT_DIR."utils/domready.js'></script>
				 <script type='text/javascript' src='".HTTP_SERVER.SCRIPT_DIR."utils/validation.js'></script>
				 <script type='text/javascript' src='".HTTP_SERVER.SCRIPT_DIR."common.js'></script>";
  $styles = "<link rel='stylesheet' type='text/css' href='".HTTP_SERVER.STYLE_DIR."main.css' />";
	
	//breakdown the URL
	$urlpath = ltrim($_SERVER['REQUEST_URI'], '/');
	if (strpos($urlpath, "?"))
		$urlpath = substr_replace($urlpath, "", strpos($urlpath, "?"), strlen($urlpath));
	$rewrite = $_SERVER['REQUEST_URI'];
	$url = explode('/',strtolower($rewrite));
	array_shift($url);
	//if localhost adjust url breakdown
	if ($url[0] == 'sablogawards') array_shift($url);
	//strip off any GET requests
	for($i=0; $i<count($url); $i++) 
		if(strpos($url[$i],"?")) $url[$i] = substr_replace($url[$i], "", strpos($url[$i],"?"), strlen($url[$i]));
	
	//include and setup ajax library
	include_once("projax.php");
	$projax = new Projax();
	
	/* common functions */
	include_once("functions.php");
	
?>